import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import QtQuick.Dialogs 1.3
import com.RspInterface 1.0
import "public.js" as Public
import com.ComboData 1.0


//客户风险统计查询
Item {
    id: riskCust
    property string listName: "RiskCustStatics"
    property var tabName
    //    property var branchID
    property bool bChangeWidth: false
    property bool bInital: false
    property string activeCustID: ""
    property ComboData branchModel: g_MainQuo.getMyBranchs()
    property var sectionSpace: 10
    property var kvSpace: 2
    //客户级别下拉
    property ComboData custGradeModel
    //    signal freshData
    //    onFreshData: {
    //        freshResult
    //    }

    //    onBranchIDChanged: freshData()
    ColumnLayout {
        anchors.fill: parent
        spacing: 10
        RowLayout {
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.top: parent.top
            anchors.topMargin: 10
            anchors.right: parent.right
            anchors.rightMargin: 20
            Layout.preferredHeight: 40
            spacing: 5

            Text {
                text: "客户号" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.left: parent.left
            }
            TextField {
                id: acct_No
                Layout.alignment: Qt.AlignHCenter
                Layout.preferredWidth: 130
            }

            Text {
                text: qsTr("账户类型") + colon
                anchors.left: acct_No.right
                anchors.leftMargin: sectionSpace
            }

            ComboBox {
                id: cmbAcctType
                Layout.preferredWidth: 65
                model: acctTypeModel
            }

            Text {
                text: "代理商" + colon
                anchors.left: cmbAcctType.right
                anchors.leftMargin: sectionSpace
            }
            ComboBox {
                id: cmbBranch
                model: branchModel
                Layout.preferredWidth: 260
                onCurrentIndexChanged: {
                    custGradeModel = null
                    custGradeModel = g_MainQuo.getCustGradeByBranchs(
                                branchModel.get(currentIndex))
                    for (var i = 0; i < custGradeModel.rowCount(); ++i) {
                        console.log("custGradeModel " + custGradeModel.get(i))
                    }
                }
            }
        }
        RowLayout {
            //            anchors.top: parent.top
            //            anchors.topMargin: 5
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.right: parent.right
            //            anchors.rightMargin: 20
            Layout.preferredHeight: 30
            Layout.alignment: Qt.AlignHCenter

            Text {
                text: qsTr("风险等级") + colon
            }

            ComboBox {
                id: cmbRiskGrade
                Layout.preferredWidth: 118
                model: ListModel {
                    ListElement {
                        text: " "
                        risk_grade: 01
                    }
                    ListElement {
                        text: "追保"
                        risk_grade: 21
                    }
                    ListElement {
                        text: "相对强平"
                        risk_grade: 31
                    }
                    ListElement {
                        text: "绝对强平"
                        risk_grade: 41
                    }
                }
            }

            Text {
                id: custGradeT
                text: "客户级别" + colon
                Layout.alignment: Qt.AlignHCenter
                anchors.left: cmbRiskGrade.right
                anchors.leftMargin: sectionSpace
            }
            ComboBox {
                id: cmbCustGrade
                Layout.alignment: Qt.AlignHCenter
                anchors.left: custGradeT.right
                anchors.leftMargin: kvSpace
                model: custGradeModel
                onCurrentIndexChanged: {
                    console.log("gradeName " + currentText)
                }
            }
            Item {
                Layout.fillWidth: true
            }
            Button {
                id: query
                text: "查询"
                onClicked: {
                    freshResult()
                }
            }

            //            Button {
            //                text: "关闭"
            //            }
            Item {
                Layout.preferredWidth: 25 //fillWidth把最后一个挤出去的bug补救
            }
        }
        //        GroupBox {
        //            anchors.left: parent.left
        //            anchors.leftMargin: 10
        //            anchors.right: parent.right
        //            anchors.rightMargin: 10
        //            Layout.fillHeight: true
        //            title: "试算结果"
        ListFrame {
            id: frame
            Layout.fillHeight: true
            anchors.left: parent.left
            anchors.leftMargin: 5
            anchors.right: parent.right
            anchors.rightMargin: 5

            tablelistName: listName
            onLoadHeaderFinish: bInital = true
            onGridWidthChange: {
                if (bInital && col >= 0) {
                    colCfg.setColumnWidth(listName,
                                          tableList.getColumn(col).role,
                                          tableList.getColumn(col).width)
                    bChangeWidth = true
                }
            }
            onRightClick: {
                rBtnMenu.row = row
                rBtnMenu.col = col
                rBtnMenu.model = frame.tableList.model
                rBtnMenu.popup()
            }
            Connections {
                target: colSet
                onUpdateUIColumn: {
                    if (sList == listName) {
                        bInital = false //lqh 更新表头会导致改变列宽，触发保存列宽，没有必要
                        frame.updateColumn(listName)
                        bInital = true
                    }
                }
            }
        }

        PageRow {
            id: pageRow
            anchors.left: parent.left
            anchors.leftMargin: 20
            anchors.right: parent.right
            anchors.rightMargin: 20
            Layout.preferredHeight: 30
            //                anchors.bottom: parent.bottom
            //            anchors.bottomMargin: 40
            //                anchors.top: frame.bottom
            //            anchors.topMargin: 5
        }
        //        }
        RowLayout {
            Layout.preferredHeight: 30
            spacing: 20
            Button {
                text: "设置列"
                anchors.left: parent.left
                anchors.leftMargin: 25
                onClicked: {
                    colSet.visible = true
                    //                    colSet.refList = frame
                    colSet.loadColumnCfg(listName)
                }
            }

            Button {
                anchors.left: parent.left
                anchors.leftMargin: parent.width / 2
                text: "打印"
                //                enabled: activeCustID == "" ? false : true
                //                onClicked: {
                //                    custInfo.visible = true
                //                    custInfo.acctNo = activeCustID
                //                    console.log("客户信息 ")
                //                }
            }
            Item {
                Layout.fillWidth: true
            }
        }
    }
    Button {
        text: "关闭"
        anchors.right: parent.right
        anchors.rightMargin: 25
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        onClicked: closeTab(tabName)
    }
    function getCondition() {
        var sCond = "#branch_id=" + branchModel.get(cmbBranch.currentIndex)
        if (acct_No.text.length > 0)
            sCond += "#acct_no=" + acct_No.text
        if (cmbAcctType.currentIndex !== 0)
            sCond += "#acct_type=" + cmbAcctType.model.get(
                        cmbAcctType.currentIndex).acct_type
        if (cmbRiskGrade.currentIndex !== 0)
            sCond += "#risk_grade_id=" + cmbRiskGrade.model.get(
                        cmbRiskGrade.currentIndex).risk_grade
        if (cmbCustGrade.currentIndex !== 0)
            sCond += "#cust_type=" + custGradeModel.get(
                        cmbCustGrade.currentIndex)
        console.log("getCondition " + sCond)
        return sCond
    }

    function freshResult() {
        var req = getCondition()
        req += pageRow.getReqPageInfo()

        var rsp = g_MainQuo.queryRiskCustStatics(req)
        var msg = g_MainQuo.getPopMsg()
        if (msg.length > 0) {
            tipInput.text = msg
            tipInput.open()
            return
        }
        frame.tableList.model = rsp.getAlmModel("alm_result", listName)

        pageRow.recordCount = rsp.get("total_record")
        pageRow.curPage = rsp.get("page_no")
    }
    Timer {
        id: timer_save_column
        interval: 50000
        repeat: true
        triggeredOnStart: true
        running: true
        onTriggered: {
            if (!bChangeWidth)
                return
            bChangeWidth = false
            colCfg.saveColumnWidth()
            console.log("save_column ")
        }
    }
}
